package com.kly.sc.api.application.utils;

import com.fqgj.common.api.Response;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.sc.api.infras.enums.RestCodeEnum;
import com.kly.sc.api.infras.exception.ApplicationException;

import java.util.Objects;

/**
 * RPC 调用util类
 * @author linqu
 */
public class RpcUtils {

    private static Log logger = LogFactory.getLog(RpcUtils.class);

    /**
     * RPC调用结果检查
     * @param response
     * @param <T>
     * @return 返回调用成功结果
     */
    public static <T> T getResult(Response<T> response) {
        if (Objects.isNull(response)) {
            throw new ApplicationException(RestCodeEnum.RPC_INVOKE_ERROR, "The RPC invoke failed!");
        }
        if (!response.isSuccess()) {
            throw new ApplicationException(response.getCode(),  response.getMsg());
        }
        return response.getData();
    }
}
